package com.example.wangzh.developkit.widget;

import android.content.Context;
import androidx.appcompat.widget.LinearLayoutCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.model.TwoWheelData;
import com.example.wangzh.developkit.widget.wheelPicker.WheelPicker;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.wangzh.developkit.widget.wheelPicker.WheelPicker.SCROLL_STATE_DRAGGING;
import static com.example.wangzh.developkit.widget.wheelPicker.WheelPicker.SCROLL_STATE_IDLE;

/**
 * Created by wangzh on 2017/11/13.
 */

public class TwoWheel extends LinearLayoutCompat {
    @BindView(R.id.wheel_first)
    WheelPicker wheel_first;
    @BindView(R.id.wheel_second)
    WheelPicker wheel_second;

    //判断所有滚轮已滚动结束，如果没有，应当禁止获取选中数据
    boolean scrollCompleted = true;
    List<TwoWheelData> twoWheelDataList;

    public TwoWheel(Context context) {
        this(context, null);
    }

    public TwoWheel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TwoWheel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.custom_two_wheel, this);
        ButterKnife.bind(this);
        initWheelPicker(wheel_first);
        initWheelPicker(wheel_second);
        addListenerToWheelPicker();
    }

    public void setDataList(List<TwoWheelData> twoWheelDataList) {
        this.twoWheelDataList = twoWheelDataList;
        refreshData();
    }

    private void refreshData() {
        wheel_first.setData(getFirstNameList());
        wheel_second.setSelectedItemPosition(0);
        wheel_second.setData(getSecondNameList(0));
    }

    private void addListenerToWheelPicker() {
        wheel_first.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                wheel_second.setData(getSecondNameList(position));
                wheel_second.setSelectedItemPosition(0);
            }
        });
    }

    private List getFirstNameList() {
        List<String> firstNameList = new ArrayList<>();
        for (int i = 0; i < twoWheelDataList.size(); i++) {
            TwoWheelData.WheelDataFirst wheelDataFirst =
                    twoWheelDataList.get(i).getWheelDataFirst();
            StringBuffer sb = new StringBuffer(wheelDataFirst.getName());
            firstNameList.add(sb.toString());
        }
        return firstNameList;
    }

    private List getSecondNameList(int position) {
        List<String> secondNameList = new ArrayList<>();
        if (position < 0 || position > twoWheelDataList.size() - 1) {
            return secondNameList;
        }
        for (int i = 0; i < twoWheelDataList.get(position).getWheelDataSecondList().size(); i++) {
            TwoWheelData.WheelDataSecond wheelDataSecond = (TwoWheelData.WheelDataSecond)
                    twoWheelDataList.get(position).getWheelDataSecondList().get(i);
            StringBuffer sb = new StringBuffer(wheelDataSecond.getName());
            secondNameList.add(sb.toString());
        }
        return secondNameList;
    }

    private void initWheelPicker(WheelPicker wheel) {
        wheel.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener() {
            @Override
            public void onWheelScrolled(int offset) {

            }

            @Override
            public void onWheelSelected(int position) {

            }

            @Override
            public void onWheelScrollStateChanged(int state) {
                if (state == SCROLL_STATE_IDLE) {
                    scrollCompleted = true;
                } else if (state == SCROLL_STATE_DRAGGING) {
                    scrollCompleted = false;
                }
            }
        });
    }

}
